package com.hurui.xiaoming.fragment.detailedlist;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.repaymentdetails.RepaymentDetailsActivity;
import com.hurui.xiaoming.adapter.DetailedListAdapter;
import com.hurui.xiaoming.bean.Li_MyList_Result;
import com.hurui.xiaoming.fragment.BaseFragment;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hurui on 2017/7/16.
 */

public class DetailedListFragment extends BaseFragment {

    private static int INIT_PAGE = 1;
    private String oi_state;
    private XRecyclerView detailed_list_rv;

    private List<Li_MyList_Result.DataBean> dataLists;

    private DetailedListAdapter adapter;

    private View rootView;
    private Map<String, String> map=new HashMap<>();

    int page = INIT_PAGE;

    public static DetailedListFragment newInstance(String state) {

        Bundle args = new Bundle();

        DetailedListFragment fragment = new DetailedListFragment();
        args.putString("oi_state", state);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        oi_state = getArguments().getString("oi_state");
        Log.i("状态值","" + oi_state.toString());
        page = INIT_PAGE;
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_detailed_list, null);
            initView();
            initAdapter();
            initAction();
        }
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        loadData(page);
    }

    private void initAdapter() {
        dataLists = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            dataLists.add(new DetailedListBean());
//        }
        adapter = new DetailedListAdapter(mContext, dataLists);
        detailed_list_rv.setLayoutManager(new LinearLayoutManager(mContext));
        detailed_list_rv.setAdapter(adapter);

        adapter.setListener(new DetailedListAdapter.DerailedListListener() {
            @Override
            public void ClickInfoMore(int position) {
                Intent intent = new Intent(mContext, RepaymentDetailsActivity.class);
                intent.putExtra(PamaryKeys.OI_ID,adapter.getItemId(position)+"");
                onOpen(intent);
            }
        });
    }

    @Override
    public void initView() {
        detailed_list_rv = (XRecyclerView) rootView.findViewById(R.id.detailed_list_rv);
    }

    @Override
    public void initAction() {

        detailed_list_rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                loadData(INIT_PAGE);

            }

            //下拉加载
            @Override
            public void onLoadMore() {
                page ++;
                loadData(page);
            }
        });


    }
    public void loadData(int page){
        //+ WriteReadSharedPrefs.readUserID(mContext)
        map.clear();
        //map.put(PamaryKeys.UI_ID, "" + WriteReadSharedPrefs.readUserID(mContext));
        map.put(PamaryKeys.UI_ID, "" + WriteReadSharedPrefs.readUserID(mContext));
        map.put("oi_state", oi_state);
        map.put("page_num",String.valueOf(page));

        apiSevice.onHTttp_myOrder(map).enqueue(new Callback<Li_MyList_Result>() {
            @Override
            public void onResponse(Call<Li_MyList_Result> call, Response<Li_MyList_Result> response) {
                Li_MyList_Result body = response.body();
                List<Li_MyList_Result.DataBean> data = body.getData();
                if(data.size() > 0){
                    adapter.refreshData(data,oi_state);

//                Toast.makeText(getActivity(),"请求成功",Toast.LENGTH_SHORT).show();
                }
                detailed_list_rv.refreshComplete();
                detailed_list_rv.loadMoreComplete();
            }

            @Override
            public void onFailure(Call<Li_MyList_Result> call, Throwable t) {
                Log.i("清单",t.getMessage());
                onToast(t.getMessage());
                detailed_list_rv.refreshComplete();
                detailed_list_rv.loadMoreComplete();

            }
        });
    }
}
